package com.zshan.clinic.common.util.list;

import com.zshan.clinic.common.util.string.StringUtil;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ListUtil {


    /**
     * 数字类型的数组在转化为list集合
     *
     * @param array
     * @return
     */
    public static List<Long> arrToLongList(String[] array) {
        List<Long> list = new ArrayList<>();
        for (String arr : array) {
            list.add(Long.parseLong(arr));
        }
        return list;
    }

    /**
     * 数字类型的数组在转化为list集合
     *
     * @param array
     * @return
     */
    public static List<Integer> arrToIntList(String[] array) {
        List<Integer> list = new ArrayList<>();
        for (String arr : array) {
            list.add(Integer.parseInt(arr));
        }
        return list;
    }

    /**
     * 转化为为集合
     *
     * @param strList
     * @return
     */
    public static List<Integer> strToIntList(List<String> strList) {
        List<Integer> list = new ArrayList<>();
        for (String s : strList) {
            list.add(Integer.parseInt(s));
        }
        return list;
    }

    public static List<String> strToStrList(String str) {
        if (StringUtil.isBlank(str)) {
            return new ArrayList<>();
        }
        String[] strings = str.replace("，", ",").split(",");
        return Arrays.asList(strings);
    }


    /**
     * 字符串华为为集合
     *
     * @param str
     * @return
     */
    public static List<Integer> strToList(String str) {
        if (StringUtil.isBlank(str)) {
            return new ArrayList<>();
        }
        String[] strings = str.replace("，", ",").split(",");
        return arrToIntList(strings);
    }

    /**
     * 字符串华为为集合
     *
     * @param str
     * @return
     */
    public static List<Long> strToLongList(String str) {
        if (StringUtil.isBlank(str)) {
            return new ArrayList<>();
        }
        String [] strings = str.replace("，", ",").split(",");
        return arrToLongList(strings);
    }

    /**
     * 集合转字符串
     *
     * @param list
     * @return
     */
    public static String intListToStr(List<Integer> list) {
        if (CollectionUtils.isEmpty(list)) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for (Integer value : list) {
            sb.append(value);
            sb.append(",");
        }
        String result = sb.toString();
        if (result.endsWith(",")) {
            result = result.substring(0, result.lastIndexOf(","));
        }
        return result;
    }


    /**
     * 集合转字符串
     *
     * @param list
     * @return
     */
    public static String strListToStr(List<String> list) {
        if (CollectionUtils.isEmpty(list)) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for (String value : list) {
            sb.append(value);
            sb.append(",");
        }
        String result = sb.toString();
        if (result.endsWith(",")) {
            result = result.substring(0, result.lastIndexOf(","));
        }
        return result;
    }


    public boolean contains(List<String> list,String obj){
        if(StringUtil.isBlank(obj) || CollectionUtils.isEmpty(list)){
            return false;
        }
        for(String item : list){
            if(item.equals(obj)){
                return true;
            }
        }
        return false;
    }
}













